package com.xone.collaboration.service.impl;

import com.alibaba.fastjson.JSON;
import com.xone.collaboration.service.OperationProcess;
import com.xone.collaboration.service.OperationService;
import com.xone.collaboration.service.impl.process.OperationContext;
import com.xone.collaboration.service.vo.OperationResult;
import com.xone.collaboration.service.vo.OperationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OperationServiceImpl implements OperationService {

    @Resource(name = "operationProcessChain")
    private OperationProcess operationProcessChain;

    @Override
    public OperationResult operate(OperationVO operationVO) {
        try{
            log.info("operation start,op content:{}", JSON.toJSON(operationVO));
            OperationContext context = new OperationContext();
            BeanUtils.copyProperties(operationVO,context);
            context.setNeedRun(true);
            return operationProcessChain.process(context);
        }catch (Exception e){
            log.error("operation error:",e);
            return OperationResult.of(false,e.getMessage());
        }
    }
}
